package com.houtai.eduservice.controller.front;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.houtai.commonutils.Result;
import com.houtai.commonutils.ordersvo.CourseWebVoOrder;
import com.houtai.eduservice.entity.EduCourse;
import com.houtai.eduservice.entity.chapter.ChapterVo;
import com.houtai.eduservice.entity.frontvo.CourseFrontVo;
import com.houtai.eduservice.entity.frontvo.CourseWebVo;
import com.houtai.eduservice.service.EduChapterService;
import com.houtai.eduservice.service.EduCourseService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;
import java.util.Map;

/**
 * @ClassName CourseFrontController
 * @Description 课程列表
 * @Author xst
 * @Date 2022/12/11 21:14
 * @Version 1.0
 **/
@RestController
@RequestMapping("/eduservice/coursefront")
@CrossOrigin
public class CourseFrontController {

    @Autowired
    private EduCourseService courseService;

    @Autowired
    private EduChapterService chapterService;
    /**
     * 课程分页查询
     * @param page
     * @param limit
     * @param courseFrontVo
     * @return
     */
    @PostMapping("getFrontCourseList/{page}/{limit}")
    public Result getFrontCourseList(@Valid @PathVariable long page, @PathVariable long limit,
                                     @RequestBody(required = false) CourseFrontVo courseFrontVo){
        Page<EduCourse> pageCourse = new Page<>(page,limit);
        Map<String,Object> map = courseService.getCourseFrontList(pageCourse,courseFrontVo);
        return Result.ok().data(map);
    }

    //课程详情的方法
    @GetMapping("getFrontCourseInfo/{courseId}")
    public Result getFrontCourseInfo(@Valid @PathVariable Integer courseId){
        //根据课程id，编写sql语句查询课程信息
        CourseWebVo courseWebVo = courseService.getBaseCourseInfo(courseId);

        //根据课程id查询章节小节
        List<ChapterVo> chapterVideoList = chapterService.getChapterVideoById(courseId);

        return Result.ok().data("courseWebVo",courseWebVo).data("chapterVideoList",chapterVideoList);
    }

    /**
     * 根据课程id获取课程信息
     * @param id
     * @return
     */
    @PostMapping("getCourseInfoOrder/{id}")
    public CourseWebVoOrder getCourseInfoOrder(@Valid @PathVariable Integer id){
        CourseWebVo courseInfo = courseService.getBaseCourseInfo(id);
        CourseWebVoOrder courseWebVoOrder = new CourseWebVoOrder();
        BeanUtils.copyProperties(courseInfo,courseWebVoOrder);
        return courseWebVoOrder;
    }

}
